
$(function(){
	var nowDate = new Date();
	$("#beginDate").html(getMonthStartDate());
	$("#endDate").html(dateTimeFormat("yyyy-MM-dd",nowDate));

	//第一次加载
	createmainTable()

	//确定按钮
	$("button#selectorBtnOK, #confirmBtn").click(function(){
		var data = {
			accountUser: $('#qryAccount').val(),
			accountAlias: $('#qryAlias').val(),
			beginDate: $('#selectorBeginDateShow').html(),
			endDate: $('#selectorEndDateShow').html(),
			qryRes: $('#qryRes').val(),
			qryState: $('#qryState').val(),
			qryResQC: $('#selectorPayShow').attr('data-value')
		};
		mui.back();
		createmainTable(data);
	});
	//-----------------------------查询界面开始---------------------------------------------------
	//为开始日期和结束日期设置默认值
	// $("#selectorBeginDateShow").html(getMonthStartDate());
	// $("#selectorEndDateShow").html(dateTimeFormat("yyyy-MM-dd",nowDate));
	//日期选择器
	$(".myPicker").click(function(){
		var self = this;
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//开始日期选择器
			case 'selectorBeginDate':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDate':
				datePicker.show(function(items) {
					$("#selectorEndDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//期初
			case 'selectorPay':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '是'},{value: '0' ,text: '否'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorPayShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});


	//清空按钮
	$("button#selectorBtnCancel").click(function(){
		$('#accountUserInput').val('');
		$('#accountAliasInput').val('');
		$('#selectorBeginDateShow').html('');
		$('#selectorEndDateShow').html('');
	});


//-----------------------------查询界面结束---------------------------------------------------
});
//-----------------------------主界面开始---------------------------------------------------

function createmainTable(params){
	params = params || {};
	$("#accountUserSpan").html(params.accountUser || '');
	$("#accountAliasSpan").html(params.accountAlias|| '');
	$("#qryResD").html(params.qryRes|| '');
	$("#qryStateD").html(params.qryState|| '');
	$('#qryResQCD').html($('#selectorPayShow').html())
	$("#beginDate").html(params.beginDate|| '');
	$("#endDate").html(params.endDate|| '');

	$('#table').html("");
	layui.use('table', function() {
		var table = layui.table;
		table.render({
			elem: '#table'
			, limit: 20
			// ,skin: 'line'
			, even: true
			, size: 'sm'
			, height: $(window).height() - $('.head-search').height() - 120
			, url: url1 + Utils.Api.rptHbzjGet //数据接口
			, where: {
				accessid: window.localStorage.accessid || '',
				field: 'kcAccountId',
				direction: 'ASC',
				qryDateB: params.beginDate|| '',
				qryDateE: params.endDate|| '',
				qryAccount: params.accountUser || '',
				qryAlias: params.accountAlias || '',
				qryRes: params.qryRes || '',
				qryState: params.qryState || '',
				qryResQC: params.qryResQC || 0
			}
			,request: {
				pageName: 'pageIndex' //页码的参数名称，默认：page
				,limitName: 'limit' //每页数据量的参数名，默认：limit
			}
			, response: {
				statusName: 'hasError', //数据状态的字段名称，默认：code
				statusCode: false, //成功的状态码，默认：0
				msgName: 'error', //状态信息的字段名称，默认：msg
				countName: 'results', //数据总数的字段名称，默认：count
				dataName: 'rows' //数据列表的字段名称，默认：data
			}
			, page: true //开启分页
			, cols: [[ //表头
				{field: 'accountAlias', title: '仓库或账号', align: 'center', width: 100},
				{field: 'resName', title: '资源名称', align: 'center', width: 100},
				{field: 'stateName', title: '资源状态', align: 'center',width: 100},
				{field: 'qtySign', title: '结存数量', align: 'center',width: 100},
				{field: 'price', title: '结存单价',align: 'right', width: 100, templet: function (d) {
					return !d.summary?  Utils.formatMoney(d.price): '<span class="font-bold">' +Utils.formatMoney(d.price)+'</span>'
				}},
				{field: 'amountSign', title: '结存金额',align: 'right', width: 100, templet: function (d) {
					return !d.summary?  Utils.formatMoney(d.amountSign): '<span class="font-bold">' +Utils.formatMoney(d.amountSign)+'</span>'
				}},
				{field: '', title: '操作',templet: '#titleTpl', align: 'center', width: 100},
			]]
			,
			sums: true
		});
	})
}


function Detail(kcAccountId,kcMxUnitId,kcMxResId, kcMxStateId ){
	//账户资金余额明细
	var detailParams = {
		kcAccountId: kcAccountId,
		kcMxUnitId: kcMxUnitId,
		kcMxResId: kcMxResId,
		kcMxStateId: kcMxStateId
	}
	mui.openWindow({
		url: 'resourceBalanceDetail.html',
	})
	window.localStorage.resourceParams = JSON.stringify(detailParams)
}
